/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 1/1/2003
 * Time: 1:39 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Collections;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices; 
using System.Drawing.Imaging;
using System.Configuration;
using popLib;
using popLib.ExtensionClass;
using System.Reflection;

namespace MailCheckerGadget
{
	/// <summary>
	/// Description of Start.
	/// </summary>
	public class Start: SidebarGadget
	{	
		string passKey = string.Format( "dGhvaWdpYW50cm9pc{0}bWF1Y2hpY29ubGFpbmh{1}1bmdre{2}pZW0=", "XVh", "", "W5" );
		public enum optionsType { Host, Username, Password, SSL, AlwaysNotifyNewMail, RequestDelay }
		
		public Start(SidebarContainer sidebarContainer):base(sidebarContainer){
						
			ini_config.setValue( "Setting", "Name", "Mail checker Gadget" );
			ini_config.setValue( "Setting", "Description", "Check new mail from IMAP server." );
			
			
			Hashtable options = new Hashtable();
			options.Add( optionsType.Host.ToString() 	 , "" );
			options.Add( optionsType.Username.ToString() , "" );
			options.Add( optionsType.Password.ToString() , "" );
			options.Add( optionsType.SSL.ToString() 	 				, "0" );
			options.Add( optionsType.AlwaysNotifyNewMail.ToString() 	, "0" );
			options.Add( optionsType.RequestDelay.ToString() , "30" );
			ini_config.getValue( "Setting", "Options", StringLib.deParseOption(options) );
									
		}
		
		#region Functions
		
		public override object InitGadget(){
			if( control == null ){				
				control = new MailCheckerPanel(this);
				UpdateOptions(false);				
			}
			return control;
		}		
						
		public override void ViewOptions(){
			if( sidebarGadgetOption == null ) sidebarGadgetOption = new Options(this);
			sidebarGadgetOption.Show();
		}		
		
		public override void UpdateOptions(bool reloadSidebar){
	    	if( control == null ) return;
	    	control.StartComponent();
	    }
		
	    public string getPasskey(){ return passKey; }
	    #endregion
		
		#region Properties		
	    public override Hashtable Options {
	    	get{ 
	    		Hashtable tmpOptions = StringLib.parseOption( ini_config.getValue( "Setting", "Options") );
	    		string password = tmpOptions[optionsType.Password.ToString()].ToString().Replace("[.:AND:.]", "=");
	    		string key      = StringLib.decodeFrom64(passKey);
	    		password = StringLib.decodeFrom64( password, key );
	    		tmpOptions[optionsType.Password.ToString()] = password;
	    		return tmpOptions;
	    	}
	    	set{
	    		Hashtable options = value;
	    		options[optionsType.Password.ToString()] = StringLib.encodeTo64( options[optionsType.Password.ToString()].ToString(), 
	    		                                                                  StringLib.decodeFrom64(passKey) ).Replace("=","[.:AND:.]");
	    		ini_config.setValue( "Setting", "Options", StringLib.deParseOption(options) );
	    		UpdateOptions(false);
	    	}
	    }
	    #endregion
		
	}
}
